package com.lanou.zhihu.service.impl;

import com.lanou.zhihu.entity.Answer;
import com.lanou.zhihu.entity.Comment;
import com.lanou.zhihu.entity.Question;
import com.lanou.zhihu.entity.User;
import com.lanou.zhihu.mapper.AnswerMapper;
import com.lanou.zhihu.mapper.CommentMapper;
import com.lanou.zhihu.mapper.QuestionMapper;
import com.lanou.zhihu.mapper.UserMapper;
import com.lanou.zhihu.service.RemindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/11/1.
 */
@Service
public class RemindServiceImpl implements RemindService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    CommentMapper commentMapper;

    @Override
    public List<Map<String, Object>> answerRemindByUid(Integer uid) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Question> questionList = questionMapper.findByUid(uid);
        for (Question question : questionList) {
            List<Answer> answerList = answerMapper.findByQidState(question.getId());
            for (Answer answer : answerList) {
                Map<String, Object> map = new HashMap<>();
                User user = userMapper.findById(answer.getUid());
                user.setPassword("");
                map.put("answerUser", user);
                map.put("answerQuestion", question);
                map.put("answer", answer);
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public boolean readRemindByAid(Integer aid) {
        Integer result = answerMapper.updateStateByAid(aid);
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Map<String, Object>> commentRemindByUid(Integer uid) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Answer> answerList = answerMapper.findbyUid(uid);
        for (Answer answer : answerList) {
            List<Comment> commentList = commentMapper.findByAidState(answer.getId());
            Question question = questionMapper.findById(answer.getQid());
            for (Comment comment : commentList) {
                User user = userMapper.findById(comment.getUid());
                Map<String, Object> map = new HashMap<>();
                map.put("answer", answer);
                map.put("comment", comment);
                map.put("question", question);
                map.put("user", user);
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public boolean readCommentRemindByCid(Integer cid) {
        Integer result = commentMapper.updateStateByCid(cid);
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }
}
